package netWork;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;

public class UdpReceiverA {

    /**
     * 接收端A
     */
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9999);
        DatagramPacket requestPacket = new DatagramPacket(new byte[4096],0,4096);
        socket.receive(requestPacket);//接受发送来的数据
        String request = new String(requestPacket.getData(),0, requestPacket.getLength());
        //封装发送包
        String response = "好的,明天见";
        DatagramPacket responsePacket = new DatagramPacket(response.getBytes(StandardCharsets.UTF_8),0,response.getBytes(StandardCharsets.UTF_8).length,requestPacket.getAddress(),requestPacket.getPort());
        socket.send(responsePacket);
        System.out.printf("[%s %d],req: %s  resp : %s \n",requestPacket.getAddress().getHostAddress(),requestPacket.getPort(),request,response);
        socket.close();

    }
}
